package com.xinweisi.dataserver.module.memberdata.tradeflow;

import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.dto.MemberTradeFlowQueryDTO;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.vo.MemberTradeFlowResultVO;
import com.xinweisi.dataserver.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * MemberTradeFlowController
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/21 0021 14:33
 */
@Slf4j
@RestController
@RequestMapping("/api/memberTradeFlow")
public class MemberTradeFlowController {
    @Autowired
    MemberTradeFlowService memberTradeFlowService;
    @Value("${startDBName}")
    private String OldDBName;
    @ApiOperation(value = "分页查会员交易流水", notes = "分页查会员交易流水")
    @RequestMapping(value = "/getMemberTradeFlow",method= RequestMethod.POST)
    public ResponseDTO<MemberTradeFlowResultVO> getMemberTradeFlow(@RequestBody @Valid MemberTradeFlowQueryDTO queryDTO) {
        log.info("----------分页查会员交易流水----------");
        List<String> DBNameList = new ArrayList<String>();
        String dateStart=queryDTO.getStartTime();
        if(dateStart==null||"".equals(dateStart)){
            dateStart = DateUtil.getDateAll(new Date());
        }
        String dateStartStr =dateStart.substring(0,dateStart.indexOf(" ")).replace("-","");
        String date1=OldDBName.substring(OldDBName.indexOf("_")+1);
        if(dateStartStr.compareTo(date1)<0){
            dateStartStr = date1;
        }
        String DBName = "";
        String dateEnd=queryDTO.getEndTime();
        if(dateEnd==null||"".equals(dateEnd)){
            dateEnd = DateUtil.getDateAll(new Date());
        }
        String dateEndStr =dateEnd.substring(0,dateEnd.indexOf(" ")).replace("-","");
        List<String> list = DateUtil.getBetweenDates(dateStartStr,dateEndStr);
        for (String str:list) {
            DBName = "daylog_"+str;
            DBNameList.add(DBName);
        }
        queryDTO.setDBNameList(DBNameList);
        return memberTradeFlowService.getMemberTradeFlow(queryDTO);
    }
}
